import {AxiosPromise} from "axios";
import {PageQuery, Result} from "@/model";
import {Album} from "@/api/album/types";
import request from "@/utils/request";

/**
 * 向ai提问
 * @param question 问题
 * @returns ai提问
 */
export function aiQuestion(question: string): Promise<any> {
    return fetch("/api/aiChat/question?question=" + question)
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            // return response.text();
        })
        .catch(error => {
            console.error('There was a problem with the fetch operation:', error);
            throw error;
        });
}

export async function fetchData(question: string, cb: (data: string) => void) {

    let response = await fetch("/api/aiChat/question?question=" + question, {
      method: "POST",
      headers: {
        "Content-Type": "application/octet-stream",
      },
    });
  
    if (!response.ok) {
      console.error("HTTP request failed with status: " + response.status);
      return;
    }
    let body = response.body;
    if (body === null) {
      console.error("HTTP response body is empty");
      return;
    }
    // 获取 ReadableStream
    const readableStream = body.getReader();
  
    // 逐步读取数据
    while (true) {
      const {done, value} = await readableStream.read();
      if (done) {
        // 读取完成
        break;
      }
      // 处理每个数据块（value 是 Uint8Array）
      const dataChunk = new TextDecoder().decode(value);
      cb(dataChunk)
    }
  }











/**
 * 获取记录列表
 * @returns 获取记录列表
 */
export function getRecordList(params: PageQuery): AxiosPromise<Result<any>> {
    return request({
        url: "/aiAuestionRrecord/list",
        method: "get",
        params
    });
}

/**
 * 获取标签列表
 * @returns 获取标签列表
 */
export function getTagList(params: PageQuery): AxiosPromise<Result<any>> {
    return request({
        url: "/aiTag/list",
        method: "get",
        params
    });
}
